package com.mxpio.erp.purchase.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.purchase.entity.Supply;
import com.mxpio.erp.common.purchase.service.SupplyService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "SupplyController", description = "供应商数据接口")
@RestController
@RequestMapping("/erp/purc/supply/")
public class SupplyController {

    @Autowired
    private SupplyService supplyService;

    @GetMapping("list")
    @Operation(summary = "供应商列表", description = "获取供应商列表（无分页）", method = "GET")
    public Result<List<Supply>> list(Criteria criteria) throws UnsupportedEncodingException {
        List<Supply> supplys = supplyService.list(Supply.class, criteria);
        return Result.OK(supplys);
    }

    @GetMapping("page")
    @Operation(summary = "供应商列表", description = "获取供应商列表（分页）", method = "GET")
    public Result<Page<Supply>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Supply> supplys = supplyService.listPage(Supply.class, page, criteria);
        return Result.OK(supplys);
    }

    @GetMapping("list/{pnCode}")
    @Operation(summary = "根据pnCode获取供应商", description = "根据pnCode获取供应商", method = "GET")
    public Result<Supply> getById(@PathVariable(name = "pnCode", required = true) String pnCode) {
        Supply supply = supplyService.getById(Supply.class, pnCode);
        return Result.OK(supply);
    }

    @PostMapping("add")
    @Operation(summary = "新增供应商", description = "新增供应商", method = "POST")
    public Result<Supply> add(@RequestBody Supply supply) {
        supplyService.save(supply);
        return Result.OK(supply);
    }
    
    @PutMapping("edit")
    @Operation(summary = "编辑供应商", description = "编辑供应商", method = "PUT")
    public Result<Supply> edit(@RequestBody Supply supply) {
        supplyService.update(supply);
        return Result.OK(supply);
    }
    
    @PutMapping("assign/{username}/{pnCodes}")
    @Operation(summary = "批量分配业务员", description = "批量分配业务员", method = "PUT")
    public Result<Object> assign(@Parameter(description="用户名") @PathVariable(name = "username", required = true) String username,
    		@Parameter(description="客户编码，逗号分隔") @PathVariable(name = "pnCodes", required = true) String pnCodes) {
    	supplyService.assign(username, pnCodes);
        return Result.OK();
    }
    
    @PutMapping("transfer/{fromUser}/{toUser}")
    @Operation(summary = "业务员转移", description = "业务员转移", method = "PUT")
    public Result<Object> transfer(@Parameter(description="原业务员") @PathVariable(name = "fromUser", required = true) String fromUser,
    		@Parameter(description="新业务员") @PathVariable(name = "toUser", required = true) String toUser) {
    	supplyService.transfer(fromUser, toUser);
        return Result.OK();
    }

    @DeleteMapping("remove/{pnCodes}")
    @Operation(summary = "根据pnCode删除供应商", description = "删除供应商", method = "DELETE")
    public Result<Supply> remove(@PathVariable(name = "pnCodes", required = true) String pnCodes) {
        String[] pnCode = pnCodes.split(",");
        for(String key : pnCode){
            supplyService.remove(Supply.class, key);
        }
        return Result.OK();
    }
}
